package com.hxit.huaxiamelongarden.controller;

import com.hxit.huaxiamelongarden.domin.dto.IndexDto;
import com.hxit.huaxiamelongarden.domin.po.Content;
import com.hxit.huaxiamelongarden.service.ContentService;
import com.hxit.huaxiamelongarden.service.HotSearchService;
import com.hxit.huaxiamelongarden.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName HotSearchController
 * @description 热搜功能
 * @Author 12694
 * @Date 2025/1/29 12:22
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/hot")
@Tag(name = "热搜功能")
public class HotSearchController {
    @Autowired
    private HotSearchService hotSearchService;
    @Autowired
    private ContentService contentService;
    //显示热搜榜
    @Operation(summary = "显示热搜榜")
    @GetMapping("/list")
    public Result showHotSearch(){
        Set<Object> hotSearch = hotSearchService.getHotSearch();
        // 转换为 Set<String>
        Set<String> stringSet = new HashSet<>();
        for (Object obj : hotSearch) {
            stringSet.add(obj.toString());  // 转换每个 Object 为 String
        }
        List<String> hotWordList = stringSet.stream().toList();
        List<Content> hotList = hotWordList.stream().map(hotWord -> {
            IndexDto indexDto = new IndexDto();
            indexDto.setTitle(hotWord);
            List<Content> searchContent = contentService.getSearchContent(indexDto);
            if (searchContent.size() > 0) {
                return searchContent.get(0);
            }
            return null;
        }).collect(Collectors.toList());
        return Result.success(hotList);
    }
}
